# Maintainer: melak47 <melak47@gmail.com>
# Contributor: Andrew Sun <adsun701@gmail.com>
# Contributor: Beat Bolli <dev@drbeat.li>

_realname=fmt
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=10.1.1
pkgrel=1
pkgdesc="A modern formatting library for C++ (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://fmt.dev"
license=('MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
options=('staticlibs')
source=("${_realname}-${pkgver}.tar.gz::https://github.com/fmtlib/${_realname}/archive/${pkgver}.tar.gz")
sha256sums=('78b8c0a72b1c35e4443a7e308df52498252d1cefc2b08c9a97bc9ee6cfe61f8b')

build() {
  # Shared Build
  [[ -d "${srcdir}/build-${MSYSTEM}-shared" ]] && rm -rf "${srcdir}/build-${MSYSTEM}-shared"
  mkdir -p "${srcdir}/build-${MSYSTEM}-shared" && cd "${srcdir}/build-${MSYSTEM}-shared"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    -DBUILD_SHARED_LIBS=ON \
    -DFMT_TEST=OFF \
    ../${_realname}-${pkgver}

  cmake --build .

  # Static Build
  [[ -d "${srcdir}/build-${MSYSTEM}-static" ]] && rm -rf "${srcdir}/build-${MSYSTEM}-static"
  mkdir -p "${srcdir}/build-${MSYSTEM}-static" &&  cd "${srcdir}/build-${MSYSTEM}-static"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    -DBUILD_SHARED_LIBS=OFF \
    -DFMT_TEST=OFF \
    ../${_realname}-${pkgver}

  cmake --build .
}

package() {
  # Static Install
  cd "${srcdir}/build-${MSYSTEM}-static"
  DESTDIR=${pkgdir} cmake --install .

  # Shared Install
  cd "${srcdir}/build-${MSYSTEM}-shared"
  DESTDIR=${pkgdir} cmake --install .

  # Copy the source files for including
  install -m644 ${srcdir}/${_realname}-${pkgver}/src/*.cc ${pkgdir}${MINGW_PREFIX}/include/${_realname}

  # License
  install -Dm644 ${srcdir}/${_realname}-${pkgver}/LICENSE.rst ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE.rst
}
